<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="类相关函数动态创建一个类（参数：父类，类名，额外的内存空间,一般0）创建类之后需要用下边的方法注册(注册之后断点可以看到类名,不然会显示id)">
<meta property="og:type" content="article">
<meta property="og:title" content="Runtime中常用的一些方法">
<meta property="og:url" content="http://yoursite.com/2018/07/25/Runtime中常用的一些方法/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="类相关函数动态创建一个类（参数：父类，类名，额外的内存空间,一般0）创建类之后需要用下边的方法注册(注册之后断点可以看到类名,不然会显示id)">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-07-19T05:40:06.066Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Runtime中常用的一些方法">
<meta name="twitter:description" content="类相关函数动态创建一个类（参数：父类，类名，额外的内存空间,一般0）创建类之后需要用下边的方法注册(注册之后断点可以看到类名,不然会显示id)">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/07/25/Runtime中常用的一些方法/"/>





  <title>Runtime中常用的一些方法 | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/07/25/Runtime中常用的一些方法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Runtime中常用的一些方法</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-07-25T23:15:32+08:00">
                2018-07-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/InterView/" itemprop="url" rel="index">
                    <span itemprop="name">InterView</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/07/25/Runtime中常用的一些方法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/07/25/Runtime中常用的一些方法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="类相关函数"><a href="#类相关函数" class="headerlink" title="类相关函数"></a>类相关函数</h4><h5 id="动态创建一个类（参数：父类，类名，额外的内存空间-一般0）创建类之后需要用下边的方法注册-注册之后断点可以看到类名-不然会显示id"><a href="#动态创建一个类（参数：父类，类名，额外的内存空间-一般0）创建类之后需要用下边的方法注册-注册之后断点可以看到类名-不然会显示id" class="headerlink" title="动态创建一个类（参数：父类，类名，额外的内存空间,一般0）创建类之后需要用下边的方法注册(注册之后断点可以看到类名,不然会显示id)"></a>动态创建一个类（参数：父类，类名，额外的内存空间,一般0）创建类之后需要用下边的方法注册(注册之后断点可以看到类名,不然会显示id)</h5><a id="more"></a>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Class <span class="title">objc_allocateClassPair</span><span class="params">(Class superclass, <span class="keyword">const</span> <span class="keyword">char</span> *name, <span class="keyword">size_t</span> extraBytes)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="注册一个类-要在类注册之前添加成员变量-注册之前-属性-协议-方法先添加好-不能注册之后添加"><a href="#注册一个类-要在类注册之前添加成员变量-注册之前-属性-协议-方法先添加好-不能注册之后添加" class="headerlink" title="注册一个类(要在类注册之前添加成员变量),注册之前,属性,协议,方法先添加好,不能注册之后添加"></a>注册一个类(要在类注册之前添加成员变量),注册之前,属性,协议,方法先添加好,不能注册之后添加</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">objc_registerClassPair</span><span class="params">(Class cls)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="销毁一个类-动态创建一个类之后不需要可以用这方法销毁"><a href="#销毁一个类-动态创建一个类之后不需要可以用这方法销毁" class="headerlink" title="销毁一个类(动态创建一个类之后不需要可以用这方法销毁)"></a>销毁一个类(动态创建一个类之后不需要可以用这方法销毁)</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">objc_disposeClassPair</span><span class="params">(Class cls)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="获取isa指向的Class"><a href="#获取isa指向的Class" class="headerlink" title="获取isa指向的Class"></a>获取isa指向的Class</h5><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class object_getClass(id obj) 放对象时获取 类对象,放类对象时获取元类对象</span><br></pre></td></tr></table></figure>
<h5 id="这只isa指向的Class"><a href="#这只isa指向的Class" class="headerlink" title="这只isa指向的Class"></a>这只isa指向的Class</h5><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class object_setClass(id obj, Class cls) 比如person的isa修改成Car,那么调用[person run]时候回去Car里找方法</span><br></pre></td></tr></table></figure>
<h5 id="判断一个OC对象是否Class"><a href="#判断一个OC对象是否Class" class="headerlink" title="判断一个OC对象是否Class"></a>判断一个OC对象是否Class</h5><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">BOOL object_isClass(id obj) 返回传入的对象 是否是类对象,还是实例对象</span><br></pre></td></tr></table></figure>
<h5 id="判断一个类Class是否为元类"><a href="#判断一个类Class是否为元类" class="headerlink" title="判断一个类Class是否为元类"></a>判断一个类Class是否为元类</h5><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">BOOL class_isMetaClass(Class cls)返回传入类对象是否是普通的类对象,元类</span><br></pre></td></tr></table></figure>
<h5 id="获取父类"><a href="#获取父类" class="headerlink" title="获取父类"></a>获取父类</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Class <span class="title">class_getSuperclass</span><span class="params">(Class cls)</span></span></span><br></pre></td></tr></table></figure>
<h4 id="成员变量相关"><a href="#成员变量相关" class="headerlink" title="成员变量相关"></a>成员变量相关</h4><h5 id="获取一个实例变量信息"><a href="#获取一个实例变量信息" class="headerlink" title="获取一个实例变量信息"></a>获取一个实例变量信息</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Ivar class_getInstanceVariable(Class cls, const char *name)  </span><br><span class="line">Ivar ageIvar = class_getInstanceVariable([MJPerson class], &quot;_age&quot;);</span><br><span class="line">NSLog(@&quot;%s %s&quot;, ivar_getName(ageIvar), ivar_getTypeEncoding(ageIvar));</span><br></pre></td></tr></table></figure>
<h5 id="拷贝实例变量列表（最后需要调用free释放-常用"><a href="#拷贝实例变量列表（最后需要调用free释放-常用" class="headerlink" title="拷贝实例变量列表（最后需要调用free释放,常用 )"></a>拷贝实例变量列表（最后需要调用free释放,常用 )</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Ivar *<span class="title">class_copyIvarList</span><span class="params">(Class cls, <span class="keyword">unsigned</span> <span class="keyword">int</span> *outCount)</span></span></span><br><span class="line"><span class="function">    </span></span><br><span class="line"><span class="function"><span class="keyword">unsigned</span> <span class="keyword">int</span> count</span>;</span><br><span class="line">Ivar *ivars = class_copyIvarList([MJPerson class], &amp;count);</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; count; i++) &#123;</span><br><span class="line">    <span class="comment">// 取出i位置的成员变量</span></span><br><span class="line">	Ivar ivar = ivars[i];</span><br><span class="line">	NSLog(@<span class="string">"%s %s"</span>, ivar_getName(ivar), ivar_getTypeEncoding(ivar));</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">free</span>(ivars);</span><br></pre></td></tr></table></figure>
<h5 id="设置和获取成员变量的值"><a href="#设置和获取成员变量的值" class="headerlink" title="设置和获取成员变量的值"></a>设置和获取成员变量的值</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">object_setIvar</span><span class="params">(id obj, Ivar ivar, id value)</span></span></span><br><span class="line"><span class="function">id <span class="title">object_getIvar</span><span class="params">(id obj, Ivar ivar)</span></span></span><br></pre></td></tr></table></figure>
<p>#####动态添加成员变量（已经注册的类是不能动态添加成员变量的）类名,成员变量名,成员变量占用字节,内存对齐一般1,成员变量类型比如int,那么参数是@encode(int),添加之后因为没有set方法和get方法所以,可以用KVC来使用,手动创建的类不能动态添加成员变量,因为ro_t是只读的,方法可以rw_t可修改 </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">BOOL <span class="title">class_addIvar</span><span class="params">(Class cls, <span class="keyword">const</span> <span class="keyword">char</span> * name, <span class="keyword">size_t</span> size, <span class="keyword">uint8_t</span> alignment, <span class="keyword">const</span> <span class="keyword">char</span> * types)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="获取成员变量的相关信息"><a href="#获取成员变量的相关信息" class="headerlink" title="获取成员变量的相关信息"></a>获取成员变量的相关信息</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">ivar_getName</span><span class="params">(Ivar v)</span></span></span><br><span class="line"><span class="function"><span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">ivar_getTypeEncoding</span><span class="params">(Ivar v)</span></span></span><br></pre></td></tr></table></figure>
<h4 id="属性相关"><a href="#属性相关" class="headerlink" title="属性相关"></a>属性相关</h4><h5 id="获取一个属性"><a href="#获取一个属性" class="headerlink" title="获取一个属性"></a>获取一个属性</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">objc_property_t</span> class_getProperty(Class cls, <span class="keyword">const</span> <span class="keyword">char</span> *name)</span><br></pre></td></tr></table></figure>
<h5 id="拷贝属性列表（最后需要调用free释放）"><a href="#拷贝属性列表（最后需要调用free释放）" class="headerlink" title="拷贝属性列表（最后需要调用free释放）"></a>拷贝属性列表（最后需要调用free释放）</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">objc_property_t</span> *class_copyPropertyList(Class cls, <span class="keyword">unsigned</span> <span class="keyword">int</span> *outCount)</span><br></pre></td></tr></table></figure>
<h5 id="动态添加属性"><a href="#动态添加属性" class="headerlink" title="动态添加属性"></a>动态添加属性</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">BOOL <span class="title">class_addProperty</span><span class="params">(Class cls, <span class="keyword">const</span> <span class="keyword">char</span> *name, <span class="keyword">const</span> <span class="keyword">objc_property_attribute_t</span> *attributes,<span class="keyword">unsigned</span> <span class="keyword">int</span> attributeCount)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="动态替换属性"><a href="#动态替换属性" class="headerlink" title="动态替换属性"></a>动态替换属性</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">class_replaceProperty</span><span class="params">(Class cls, <span class="keyword">const</span> <span class="keyword">char</span> *name, <span class="keyword">const</span> <span class="keyword">objc_property_attribute_t</span> *attributes,<span class="keyword">unsigned</span> <span class="keyword">int</span> attributeCount)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="获取属性的一些信息"><a href="#获取属性的一些信息" class="headerlink" title="获取属性的一些信息"></a>获取属性的一些信息</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">property_getName</span><span class="params">(<span class="keyword">objc_property_t</span> property)</span></span></span><br><span class="line"><span class="function"><span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">property_getAttributes</span><span class="params">(<span class="keyword">objc_property_t</span> property)</span></span></span><br></pre></td></tr></table></figure>
<h4 id="方法相关"><a href="#方法相关" class="headerlink" title="方法相关"></a>方法相关</h4><h5 id="获得一个实例方法、类方法"><a href="#获得一个实例方法、类方法" class="headerlink" title="获得一个实例方法、类方法"></a>获得一个实例方法、类方法</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Method class_getInstanceMethod(Class cls, SEL name)</span><br><span class="line">Method class_getClassMethod(Class cls, SEL name)</span><br></pre></td></tr></table></figure>
<h5 id="方法实现相关操作"><a href="#方法实现相关操作" class="headerlink" title="方法实现相关操作"></a>方法实现相关操作</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">IMP <span class="title">class_getMethodImplementation</span><span class="params">(Class cls, SEL name)</span> </span></span><br><span class="line"><span class="function">IMP <span class="title">method_setImplementation</span><span class="params">(Method m, IMP imp)</span></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">method_exchangeImplementations</span><span class="params">(Method m1, Method m2)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="拷贝方法列表（最后需要调用free释放）"><a href="#拷贝方法列表（最后需要调用free释放）" class="headerlink" title="拷贝方法列表（最后需要调用free释放）"></a>拷贝方法列表（最后需要调用free释放）</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Method *<span class="title">class_copyMethodList</span><span class="params">(Class cls, <span class="keyword">unsigned</span> <span class="keyword">int</span> *outCount)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="动态添加方法"><a href="#动态添加方法" class="headerlink" title="动态添加方法"></a>动态添加方法</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">BOOL <span class="title">class_addMethod</span><span class="params">(Class cls, SEL name, IMP imp, <span class="keyword">const</span> <span class="keyword">char</span> *types)</span></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">(id self, SEL _cmd)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    NSLog(@<span class="string">"_____ %@ - %@"</span>, self, NSStringFromSelector(_cmd));</span><br><span class="line">&#125;</span><br><span class="line">class_addMethod(newClass, @selector(run), (IMP)run, <span class="string">"v@:"</span>);</span><br></pre></td></tr></table></figure>
<h5 id="动态替换方法"><a href="#动态替换方法" class="headerlink" title="动态替换方法"></a>动态替换方法</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">IMP <span class="title">class_replaceMethod</span><span class="params">(Class cls, SEL name, IMP imp, <span class="keyword">const</span> <span class="keyword">char</span> *types)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="获取方法的相关信息（带有copy的需要调用free去释放"><a href="#获取方法的相关信息（带有copy的需要调用free去释放" class="headerlink" title="获取方法的相关信息（带有copy的需要调用free去释放 )"></a>获取方法的相关信息（带有copy的需要调用free去释放 )</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">SEL <span class="title">method_getName</span><span class="params">(Method m)</span></span></span><br><span class="line"><span class="function">IMP <span class="title">method_getImplementation</span><span class="params">(Method m)</span></span></span><br><span class="line"><span class="function"><span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">method_getTypeEncoding</span><span class="params">(Method m)</span></span></span><br><span class="line"><span class="function"><span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="title">method_getNumberOfArguments</span><span class="params">(Method m)</span></span></span><br><span class="line"><span class="function"><span class="keyword">char</span> *<span class="title">method_copyReturnType</span><span class="params">(Method m)</span></span></span><br><span class="line"><span class="function"><span class="keyword">char</span> *<span class="title">method_copyArgumentType</span><span class="params">(Method m, <span class="keyword">unsigned</span> <span class="keyword">int</span> index)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="选择器相关"><a href="#选择器相关" class="headerlink" title="选择器相关"></a>选择器相关</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const char *sel_getName(SEL sel)</span><br><span class="line">SEL sel_registerName(const char *str)</span><br></pre></td></tr></table></figure>
<h5 id="用block作为方法实现"><a href="#用block作为方法实现" class="headerlink" title="用block作为方法实现"></a>用block作为方法实现</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">IMP <span class="title">imp_implementationWithBlock</span><span class="params">(id block)</span></span></span><br><span class="line"><span class="function">id <span class="title">imp_getBlock</span><span class="params">(IMP anImp)</span></span></span><br><span class="line"><span class="function">BOOL <span class="title">imp_removeBlock</span><span class="params">(IMP anImp)</span></span></span><br></pre></td></tr></table></figure>
<h5 id="交换方法实现"><a href="#交换方法实现" class="headerlink" title="交换方法实现"></a>交换方法实现</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">@implementation UIControl (Extension)</span><br><span class="line"></span><br><span class="line">+ (void)load</span><br><span class="line">&#123;</span><br><span class="line">    // hook：钩子函数</span><br><span class="line">    Method method1 = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));</span><br><span class="line">    Method method2 = class_getInstanceMethod(self, @selector(mj_sendAction:to:forEvent:));</span><br><span class="line">    method_exchangeImplementations(method1, method2);	//一旦交换方法方法缓存会被清空(cache_t)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)mj_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event</span><br><span class="line">&#123;</span><br><span class="line">    NSLog(@&quot;%@-%@-%@&quot;, self, target, NSStringFromSelector(action));</span><br><span class="line">    </span><br><span class="line">    // 调用系统原来的实现</span><br><span class="line">    [self mj_sendAction:action to:target forEvent:event];</span><br><span class="line">    </span><br><span class="line">//    [target performSelector:action];</span><br><span class="line">    </span><br><span class="line">//    if ([self isKindOfClass:[UIButton class]]) &#123;</span><br><span class="line">//        // 拦截了所有按钮的事件</span><br><span class="line">//</span><br><span class="line">//    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">@implementation NSMutableArray (Extension)</span><br><span class="line"></span><br><span class="line">+ (void)load</span><br><span class="line">&#123;</span><br><span class="line">    static dispatch_once_t onceToken;</span><br><span class="line">    dispatch_once(&amp;onceToken, ^&#123;</span><br><span class="line">        // 类簇：NSString、NSArray、NSDictionary，真实类型是其他类型</span><br><span class="line">        Class cls = NSClassFromString(@&quot;__NSArrayM&quot;);</span><br><span class="line">        Method method1 = class_getInstanceMethod(cls, @selector(insertObject:atIndex:));</span><br><span class="line">        Method method2 = class_getInstanceMethod(cls, @selector(mj_insertObject:atIndex:));</span><br><span class="line">        method_exchangeImplementations(method1, method2);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)mj_insertObject:(id)anObject atIndex:(NSUInteger)index</span><br><span class="line">&#123;</span><br><span class="line">    if (anObject == nil) return;</span><br><span class="line">    </span><br><span class="line">    [self mj_insertObject:anObject atIndex:index];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;NSMutableDictionary+Extension.h&quot;</span><br><span class="line">#import &lt;objc/runtime.h&gt;</span><br><span class="line"></span><br><span class="line">@implementation NSMutableDictionary (Extension)</span><br><span class="line"></span><br><span class="line">+ (void)load</span><br><span class="line">&#123;</span><br><span class="line">    static dispatch_once_t onceToken;</span><br><span class="line">    dispatch_once(&amp;onceToken, ^&#123;</span><br><span class="line">        Class cls = NSClassFromString(@&quot;__NSDictionaryM&quot;);</span><br><span class="line">        Method method1 = class_getInstanceMethod(cls, @selector(setObject:forKeyedSubscript:));</span><br><span class="line">        Method method2 = class_getInstanceMethod(cls, @selector(mj_setObject:forKeyedSubscript:));</span><br><span class="line">        method_exchangeImplementations(method1, method2);</span><br><span class="line">        </span><br><span class="line">        Class cls2 = NSClassFromString(@&quot;__NSDictionaryI&quot;);</span><br><span class="line">        Method method3 = class_getInstanceMethod(cls2, @selector(objectForKeyedSubscript:));</span><br><span class="line">        Method method4 = class_getInstanceMethod(cls2, @selector(mj_objectForKeyedSubscript:));</span><br><span class="line">        method_exchangeImplementations(method3, method4);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)mj_setObject:(id)obj forKeyedSubscript:(id&lt;NSCopying&gt;)key</span><br><span class="line">&#123;</span><br><span class="line">    if (!key) return;</span><br><span class="line">    </span><br><span class="line">    [self mj_setObject:obj forKeyedSubscript:key];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (id)mj_objectForKeyedSubscript:(id)key</span><br><span class="line">&#123;</span><br><span class="line">    if (!key) return nil;</span><br><span class="line">    </span><br><span class="line">    return [self mj_objectForKeyedSubscript:key];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/07/23/Runtime动态解析/" rel="next" title="Runtime面试题">
                <i class="fa fa-chevron-left"></i> Runtime面试题
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/07/26/架构/" rel="prev" title="架构">
                架构 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#类相关函数"><span class="nav-number">1.</span> <span class="nav-text">类相关函数</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#动态创建一个类（参数：父类，类名，额外的内存空间-一般0）创建类之后需要用下边的方法注册-注册之后断点可以看到类名-不然会显示id"><span class="nav-number">1.1.</span> <span class="nav-text">动态创建一个类（参数：父类，类名，额外的内存空间,一般0）创建类之后需要用下边的方法注册(注册之后断点可以看到类名,不然会显示id)</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#注册一个类-要在类注册之前添加成员变量-注册之前-属性-协议-方法先添加好-不能注册之后添加"><span class="nav-number">1.2.</span> <span class="nav-text">注册一个类(要在类注册之前添加成员变量),注册之前,属性,协议,方法先添加好,不能注册之后添加</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#销毁一个类-动态创建一个类之后不需要可以用这方法销毁"><span class="nav-number">1.3.</span> <span class="nav-text">销毁一个类(动态创建一个类之后不需要可以用这方法销毁)</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#获取isa指向的Class"><span class="nav-number">1.4.</span> <span class="nav-text">获取isa指向的Class</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#这只isa指向的Class"><span class="nav-number">1.5.</span> <span class="nav-text">这只isa指向的Class</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#判断一个OC对象是否Class"><span class="nav-number">1.6.</span> <span class="nav-text">判断一个OC对象是否Class</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#判断一个类Class是否为元类"><span class="nav-number">1.7.</span> <span class="nav-text">判断一个类Class是否为元类</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#获取父类"><span class="nav-number">1.8.</span> <span class="nav-text">获取父类</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#成员变量相关"><span class="nav-number">2.</span> <span class="nav-text">成员变量相关</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#获取一个实例变量信息"><span class="nav-number">2.1.</span> <span class="nav-text">获取一个实例变量信息</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#拷贝实例变量列表（最后需要调用free释放-常用"><span class="nav-number">2.2.</span> <span class="nav-text">拷贝实例变量列表（最后需要调用free释放,常用 )</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#设置和获取成员变量的值"><span class="nav-number">2.3.</span> <span class="nav-text">设置和获取成员变量的值</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#获取成员变量的相关信息"><span class="nav-number">2.4.</span> <span class="nav-text">获取成员变量的相关信息</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#属性相关"><span class="nav-number">3.</span> <span class="nav-text">属性相关</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#获取一个属性"><span class="nav-number">3.1.</span> <span class="nav-text">获取一个属性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#拷贝属性列表（最后需要调用free释放）"><span class="nav-number">3.2.</span> <span class="nav-text">拷贝属性列表（最后需要调用free释放）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#动态添加属性"><span class="nav-number">3.3.</span> <span class="nav-text">动态添加属性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#动态替换属性"><span class="nav-number">3.4.</span> <span class="nav-text">动态替换属性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#获取属性的一些信息"><span class="nav-number">3.5.</span> <span class="nav-text">获取属性的一些信息</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方法相关"><span class="nav-number">4.</span> <span class="nav-text">方法相关</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#获得一个实例方法、类方法"><span class="nav-number">4.1.</span> <span class="nav-text">获得一个实例方法、类方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#方法实现相关操作"><span class="nav-number">4.2.</span> <span class="nav-text">方法实现相关操作</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#拷贝方法列表（最后需要调用free释放）"><span class="nav-number">4.3.</span> <span class="nav-text">拷贝方法列表（最后需要调用free释放）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#动态添加方法"><span class="nav-number">4.4.</span> <span class="nav-text">动态添加方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#动态替换方法"><span class="nav-number">4.5.</span> <span class="nav-text">动态替换方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#获取方法的相关信息（带有copy的需要调用free去释放"><span class="nav-number">4.6.</span> <span class="nav-text">获取方法的相关信息（带有copy的需要调用free去释放 )</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#选择器相关"><span class="nav-number">4.7.</span> <span class="nav-text">选择器相关</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#用block作为方法实现"><span class="nav-number">4.8.</span> <span class="nav-text">用block作为方法实现</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#交换方法实现"><span class="nav-number">4.9.</span> <span class="nav-text">交换方法实现</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
